package com.internship.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.DepartmentInfo;
import com.internship.model.JobStudent;
import com.internship.model.Weeknote;
import com.internship.page.Page;
import com.internship.service.Const;
import com.internship.service.DepartmentInfoService;
import com.internship.service.JobStudentService;
import com.internship.service.WeeknoteService;
import com.internship.util.StringUtil;


@Controller
@RequestMapping("weeknote")
public class WeeknoteController {
	private final Logger logger = Logger.getLogger(WeeknoteController.class);
	
	@Autowired
	private 	WeeknoteService weeknoteService;
	@Autowired
	private DepartmentInfoService 	departmentInfoService;
	@Autowired
	private JobStudentService 	jobStudentService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}
	/**?
	 * 实习周记列表
	 * @return
	 */
	@RequestMapping("/weeknote_list.do")
	public ModelAndView getWeeknoteList() {
		return getWeeknoteList(null,null,1);
	}
	
	private List<DepartmentInfo>  departmentInfoList = null;
	@RequestMapping("/weeknote_page_list.do")
	public ModelAndView getWeeknoteList(@RequestParam String DeptCode,@RequestParam String StudentName,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("weeknote");
		HttpSession session = getSession();
		try {
			if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
				Page<Weeknote> pageList = weeknoteService.getWeeknoteList(DeptCode, StudentName,null, pageNumber);
				this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
				mv.addObject("currentPage", pageNumber);
				mv.addObject("DeptCode", DeptCode);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("departmentInfoList",departmentInfoList);
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("weeknoteList", pageList.getResultList());
			}else if(String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("3")){
				Page<Weeknote> pageList = weeknoteService.getWeeknoteList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)),StudentName, null,pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("weeknoteList", pageList.getResultList());
			}else {
				Page<Weeknote> pageList = weeknoteService.getWeeknoteList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)),StudentName,String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)), pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("weeknoteList", pageList.getResultList());
			}
			mv.addObject("StudentName", StudentName);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mv;
	}
	/**?
	 *我的周记列表
	 * @return
	 */
	@RequestMapping("/myWeeknote_list.do")
	public ModelAndView getMyWeeknoteList() {
		return getMyWeeknoteList(1);
	}
	
	@RequestMapping("/myWeeknote_page_list.do")
	public ModelAndView getMyWeeknoteList(@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("my_weeknote");
		HttpSession session = getSession();
		try {
			JobStudent jobStudent =jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
		
		if (jobStudent!=null) {
				Page<Weeknote> pageList = weeknoteService.getMyWeeknoteList(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)), pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("weeknoteList", pageList.getResultList());
			}else {
				mv.addObject("msg", "您可能还未参加实习，无法填写周记!");
				 mv.setViewName("error");
				}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mv;
	}
	/**?
	 * 周记统计
	 * @return
	 */
	@RequestMapping("/statistics_list.do")
	public ModelAndView weeknoteStatistics() {
		return weeknoteStatistics(null,0);
	}
	@RequestMapping("/statistics_page_list.do")
	public ModelAndView weeknoteStatistics(@RequestParam String DeptCode,@RequestParam int grade) {
		ModelAndView mv = new ModelAndView("weeknote_statistics");
		HttpSession session = getSession();
		try {
			if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
				DepartmentInfo departmentInfo=	departmentInfoService.getDepartmentInfo(DeptCode);
				int weeknoteSum = weeknoteService.getWeeknotePractice(DeptCode, grade);
				if (departmentInfo!=null) {
					int weeknoteCount = weeknoteService.getWeeknoteCount(departmentInfo.getXYDM(), grade);
					int uncheckNotes = weeknoteService.getWeeknoteUncheckNotes(departmentInfo.getXYDM(), grade);
					mv.addObject("weeknoteCount",weeknoteCount);
					mv.addObject("uncheckNotes",uncheckNotes);
				}else {
					int weeknoteCount = weeknoteService.getWeeknoteCount(DeptCode, grade);
					int uncheckNotes = weeknoteService.getWeeknoteUncheckNotes(DeptCode, grade);
					mv.addObject("weeknoteCount",weeknoteCount);
					mv.addObject("uncheckNotes",uncheckNotes);
				}
				
				mv.addObject("weeknoteSum",weeknoteSum);
				
				mv.addObject("DeptCode",DeptCode);
				this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
				mv.addObject("departmentInfoList",departmentInfoList);
			}else {
				int weeknoteSum = weeknoteService.getWeeknotePractice(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_NAME)), grade);
				int weeknoteCount = weeknoteService.getWeeknoteCount(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)), grade);
				int uncheckNotes = weeknoteService.getWeeknoteUncheckNotes(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)), grade);
				mv.addObject("weeknoteSum",weeknoteSum);
				mv.addObject("weeknoteCount",weeknoteCount);
				mv.addObject("uncheckNotes",uncheckNotes);
				}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		String[] gradeList = StringUtil.getCurrentGrades();
		mv.addObject("gradeList", gradeList);
		mv.addObject("grade", grade);
		return mv;
	}
	/**?
	 * 查看周记
	 * @param id
	 * @return
	 */
	@RequestMapping("/weeknote_check.do")
	public ModelAndView weeknoteCheck(@RequestParam int id) {
		ModelAndView mv = new ModelAndView("weeknote_check");
		Weeknote  weeknote = weeknoteService.getWeeknoteById(id);
		mv.addObject("weeknote", weeknote);
		return mv;
	}
	/**?
	 * 批阅周记
	 * @param id
	 * @return
	 */
	@RequestMapping("/weeknote_marking.do")
	public ModelAndView weeknoteMarking(@RequestParam int id) {
		ModelAndView mv = new ModelAndView("weeknote_marking");
		Weeknote  weeknote = weeknoteService.getWeeknoteById(id);
		mv.addObject("weeknote", weeknote);
		return mv;
	}
	@RequestMapping("/marking.do")
	@ResponseBody
	public JSONObject marking(@RequestParam int id,@RequestParam("TeacherComment")String TeacherComment  ) {
		JSONObject json = new JSONObject();
		Weeknote  weeknote = weeknoteService.getWeeknoteById(id);
		weeknote.setTeacherComment(TeacherComment);
		weeknote.setHasComment(1);
		int rel = weeknoteService.markingWeeknote(weeknote);
		json.put("result", rel>0?"success":"error");
		return json;
	}
	/**?
	 * 添加周记
	 * @return
	 */
	@RequestMapping("/weeknote_add.do")
	public ModelAndView weeknoteAdd() {
		HttpSession session = getSession();
		ModelAndView mv = new ModelAndView("my_weeknote_add");
		JobStudent jobStudent = jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
			mv.addObject("jobStudent", jobStudent.getStartTime());
		return mv;
	}
	@RequestMapping("/weeknote_save.do")
	@ResponseBody
	public JSONObject saveWeeknote(@RequestParam("dateScope")String  dateScope,@RequestParam("week") String week,@RequestParam("WorkContent") String WorkContent,@RequestParam("WorkFeel") String WorkFeel ) {
		JSONObject json = new JSONObject();
		try {
			HttpSession session = getSession();
			JobStudent jobStudent = jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
			Weeknote  weeknote = new Weeknote();
			weeknote.setDateScope(dateScope);
			weeknote.setWeekNo(week);
			weeknote.setAddTime(StringUtil.getStrDateTime());
			weeknote.setEnterpriseName(jobStudent.getEnterpriseName());
			weeknote.setGrade(jobStudent.getDQSZJ());
			weeknote.setJobStudentNumber(jobStudent.getXH());
			weeknote.setStudentName(jobStudent.getXM());
			weeknote.setWorkFeel(WorkFeel);
			weeknote.setWorkContent(WorkContent);
			weeknote.setDeptCode(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)));
			weeknote.setTeacherNumber(jobStudent.getSchoolTeacher());
			int rel = weeknoteService.addWeeknote(weeknote);
			json.put("result", rel>0?"success":"error");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return json;
	}
	/**?
	 * 编辑周记
	 * @param id
	 * @return
	 */
	@RequestMapping("/weeknote_edit.do")
	public ModelAndView weeknoteEdit(@RequestParam int id) {
		ModelAndView mv = new ModelAndView("my_weeknote_edit");
		Weeknote  weeknote = weeknoteService.getWeeknoteById(id);
		mv.addObject("weeknote", weeknote);
		return mv;
	}
	@RequestMapping("/weeknote_update.do")
	@ResponseBody
	public JSONObject updateWeeknote(@RequestParam("id")int  id,@RequestParam("WorkContent") String WorkContent,@RequestParam("WorkFeel") String WorkFeel ) {
		JSONObject json = new JSONObject();
		try {
			HttpSession session = getSession();
			Weeknote  weeknote = weeknoteService.getWeeknoteById(id);
			weeknote.setAddTime(StringUtil.getStrDateTime());
			weeknote.setWorkFeel(WorkFeel);
			weeknote.setWorkContent(WorkContent);
			int rel = weeknoteService.updateWeeknote(weeknote);
			json.put("result", rel>0?"success":"error");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return json;
	}
	/**?
	 * 我的周记-手机端
	 * @return
	 */
	@RequestMapping("/moible_myWeeknote_list.do")
	public ModelAndView getMoibleMyWeeknoteList() {
		return getMoibleMyWeeknoteList(1);
	}
	
	@RequestMapping("/moible_myWeeknote_page_list.do")
	public ModelAndView getMoibleMyWeeknoteList(@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("moible_my_weeknote");
		HttpSession session = getSession();
		try {
			if (!String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)).equals("null")) {
				JobStudent jobStudent =jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
				if (jobStudent!=null) {
						Page<Weeknote> pageList = weeknoteService.getMyWeeknoteList(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)), pageNumber);
						mv.addObject("currentPage", pageNumber);
						mv.addObject("totalPage", pageList.getTotalPage());
						mv.addObject("totalCount", pageList.getTotalCount());
						mv.addObject("weeknoteList", pageList.getResultList());
					}else {
						mv.addObject("msg", "您可能还未参加实习，无法填写周记!");
						 mv.setViewName("error");
						}
			}else {
				 mv.setViewName("login_moible");
		}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mv;
	}
	/**?
	 * 添加周记-手机端
	 * @return
	 */
	@RequestMapping("/moible_weeknote_add.do")
	public ModelAndView moibleWeeknoteAdd() {
		HttpSession session = getSession();
		ModelAndView mv = new ModelAndView("moible_my_weeknote_add");
		JobStudent jobStudent = jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
		mv.addObject("jobStudent", jobStudent.getStartTime());
		return mv;
	}
	/**?
	 * 编辑周记 -手机端
	 * @param id
	 * @return
	 */
	@RequestMapping("/moible_weeknote_edit.do")
	public ModelAndView moibleWeeknoteEdit(@RequestParam int id) {
		ModelAndView mv = new ModelAndView("moible_my_weeknote_edit");
		Weeknote  weeknote = weeknoteService.getWeeknoteById(id);
		mv.addObject("weeknote", weeknote);
		return mv;
	}
	/**?
	 * 查看周记--手机端
	 * @param id
	 * @return
	 */
	@RequestMapping("/moible_weeknote_check.do")
	public ModelAndView moibleWeeknoteCheck(@RequestParam int id) {
		ModelAndView mv = new ModelAndView("moible_weeknote_check");
		Weeknote  weeknote = weeknoteService.getWeeknoteById(id);
		mv.addObject("weeknote", weeknote);
		return mv;
	}
}